package com.cloudeasy.post.controller;

import com.cloudeasy.api.util.ResultUtil;
import com.cloudeasy.api.vo.Result;
import com.cloudeasy.api.vo.post.PushVO;
import com.cloudeasy.core.bean.JwtParser;
import com.cloudeasy.post.service.expand.PushService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/push")
public class PushController {
    private final PushService pushService;

    private final JwtParser jwtParser;

    @Autowired
    public PushController(PushService pushService, JwtParser jwtParser) {
        this.pushService = pushService;
        this.jwtParser = jwtParser;
    }

    /**
     * 获取当前用户的推送
     *
     * @param authentication {@link Authentication}
     * @return {@link List<PushVO>}的{@link Result}
     */
    @GetMapping
    public Result<List<PushVO>> ownPush(Authentication authentication) {
        String userId = (String) jwtParser.getOnBody(authentication, "userId");
        return ResultUtil.success(pushService.get(userId));
    }
}
